package com.goktech.sportsmall.service.Impl;

import com.goktech.sportsmall.mapper.OrdersDetailsMapper;
import com.goktech.sportsmall.mapper.OrdersMapper;
import com.goktech.sportsmall.mapper.ShoppingCartMapper;
import com.goktech.sportsmall.service.OrdersService;
import com.goktech.sportsmall.utils.DateUtil;
import com.goktech.sportsmall.bean.CommSortBean;
import com.goktech.sportsmall.bean.OrdersBean;
import com.goktech.sportsmall.bean.OrdersDetailsBean;
import com.goktech.sportsmall.mapper.CommSortMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired(required = false)
    private OrdersMapper ordersMapper;

    @Autowired(required = false)
    private OrdersDetailsMapper ordersDetailsMapper;

    @Autowired(required = false)
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired(required = false)
    private CommSortMapper commSortMapper;


    @Override
    public OrdersBean insertOrder(int u_id, int a_id, String a_phone, String a_address, String a_user) {


        List<CommSortBean> commSortBeanList = shoppingCartMapper.selectByUId(String.valueOf(u_id));

        System.out.println(commSortBeanList);
        System.out.println("++++++++++++++++++++++++++++++++++++");
        //根据购物车中的商品信息逐一判断库存
        for (int i = 0;i < commSortBeanList.size();i++) {
            CommSortBean commSortBean = commSortBeanList.get(i);
            //判断购物车中的商品的购买数量是否大于库存量,大于库存量则不进行购买,从当前的集合中去除
            if (commSortBean.getCs_rep() < commSortBean.getC_count()){
                //从List集合中取出某一个元素
                commSortBeanList.remove(commSortBean);
            }
        }
        //创建订单

        /**********************************************************************************************/

        System.out.println(DateUtil.getDate("yyyyMMdd"));
        String orderNo = ordersMapper.selectByCreateTime(DateUtil.getDate("yyyyMMdd"));
        /**
         订单编号的创建规则:O+年月日+四位数字
         **/
        String oNo = "O" + DateUtil.getDate("yyyyMMdd");
        if (orderNo == null){
            oNo = oNo + "0001";
        }else{
            String str = orderNo.substring(9);//截取出编码后四位
            int no = Integer.valueOf(str);//把字符串的编码转为int
            no++;
            oNo = oNo + String.format("%0" + 4 + "d",no);//进行自动补全,补齐到4位
        }
        OrdersBean ordersBean = new OrdersBean();
        ordersBean.setO_id(oNo);
        System.out.println(ordersBean.getO_id());
        ordersBean.setU_id(u_id);
        ordersBean.setA_id(a_id);
        ordersBean.setA_phone(a_phone);
        ordersBean.setA_address(a_address);
        ordersBean.setA_user(a_user);
        ordersBean.setO_create_time(DateUtil.getDate("yyyyMMddHHmmss"));
        ordersMapper.insertOrder(ordersBean);
        //创建订单详情
        OrdersDetailsBean ordersDetailsBean;
        for (CommSortBean commSortBean : commSortBeanList) {
            ordersDetailsBean = new OrdersDetailsBean();
            ordersDetailsBean.setO_id(oNo);
            ordersDetailsBean.setC_name(commSortBean.getC_name());
            ordersDetailsBean.setCs_no(commSortBean.getC_sort());
            ordersDetailsBean.setC_img(commSortBean.getCs_img());
            ordersDetailsBean.setC_count(commSortBean.getC_count());
            ordersDetailsBean.setC_price(commSortBean.getCs_price());
            ordersDetailsBean.setCs_no(commSortBean.getCs_no());
            ordersDetailsMapper.insertOrderDetails(ordersDetailsBean);
            //修改库存
//            commSortMapper.updateRep(ordersDetailsBean.getC_count()+"",commSortBean.getCs_no());
//            //删除购物车中的数据
//            shoppingCartMapper.deleteByNo(commSortBean.getSc_id());
        }
        return ordersBean;
    }

    @Override
    public Map<String, Object> selectByUid(int u_id) {
        Map<String,Object> restMap=new HashMap<>();
        restMap.put("user",ordersMapper.selectByUid(u_id));
//        System.out.println(ordersMapper.selectOidByUid(u_id));
        restMap.put("order",ordersDetailsMapper.selectByOid(ordersMapper.selectOidByUid(u_id)));
        return restMap;
    }

}
